﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace SGUI
{
    public class AtlasManager
    {
        Dictionary<string, SGAtlas> atlaes = new Dictionary<string, SGAtlas>();
        public SGAtlas GetAtlas(string atlasName)
        {
            if (!atlaes.ContainsKey(atlasName))
            {
                SGAtlas atlas = new SGAtlas();
                atlas.AtlasName = atlasName;
                //Texture
                atlas.Mat = new Material(new Material(Shader.Find("SGUIShader/Texture")));
                atlas.Mat.mainTexture =(Texture)Resources.Load("_Atlas/" + atlasName+"_Tex"); 
                //Load Sprite Config
                TextAsset txt =  (TextAsset)Resources.Load("_Atlas/"+atlasName+"_Sprites");
                string[] allSprites = txt.text.Split('\n');
                atlas.sprites = new List<UISpriteData>();
                atlaes[atlasName] = atlas;
                for (int i = 0; i < allSprites.Length;i++)
                { 
                    allSprites[i] = allSprites[i].Trim('\r');
                    if (allSprites[i] == "") continue;
                    UISpriteData spriteData = new UISpriteData();
                    string[] spriteArr = allSprites[i].Split(' ');
                    spriteData.name = spriteArr[0].Trim();
                    spriteData.x =float.Parse( spriteArr[1].Trim() );
                    spriteData.y = float.Parse(spriteArr[2].Trim());
                    spriteData.width =float.Parse( spriteArr[3].Trim());
                    spriteData.height = float.Parse(spriteArr[4].Trim());
                    atlas.sprites.Add(spriteData);
                }
            }
            return atlaes[atlasName];
        }
    }
}